<?php
//样本数
echo 'Num,','GEN,','BEH1,','BEH2,','ATT1,','ATT2,','ATT3,','ENT1,','ENT2,','ENT3,','INF1,','INF2,','IRR1,','IRR2,','IRR3,','CRE1,','CRE2,','CRE3,','PER1,','PER2,','PER3,','INT1,','INT2,','INT3,','INC1,','INC2,','NAME,','SEX,','AGE,','EDU,','INCOME,','INF3,','INC3,','BEH3,',"\n";
//生成样本量
$panelSize = $argv[1];
for ($i = 0; $i < $panelSize; $i++) {
	echo $i+1, ',';
	$q = array();
	//第1题
	$q[0] = getAllFixPercentResult($q0, array(1=>929, 2=>71), $panelSize);
	//第2题
	$q[1] = getAllFixPercentResult($q1, array(1=>30, 2=>10, 3=>18, 4=>42, 5=>103), $panelSize);
	//第3题
	if (1 == $q[1]) {
		$q[2] = 1;
	} else {
// 		$q[2] = getAllFixPercentResult($q2, array(2=>50, 3=>35, 4=>10, 5=>78), $panelSize);
		$q[2] = getFixResult($q2, array(2=>round(50*$panelSize/203), 3=>round(35*$panelSize/203), 4=>round(10*$panelSize/203), 5=>round(78*$panelSize/203)), 5, 2, '2');
	}
	$q2[$q[2]]++;
	//第4题
	//3-5 60%
	$q[3] = getFixPercentResult($q3, 0.65, 3, $panelSize, '3');
	//第5题-第6题
	if (in_array($q[3], array(3,4,5))) {
		$q[5] = getFixPercentResult($q5, 0.98, 3, round($panelSize*0.65), '5');
		$q[4] = mt_rand(1,2);
	} else {
		$q[5] = mt_rand(1,2);
		$q[4] = mt_rand(3,5);
	}
	$q[4] = flip($q[4]);
	
	//第7题
	//3-5 65%
	$q[6] = getFixPercentResult($q6, 0.85, 3, $panelSize, '6');
	//第8题-第9题
	if (in_array($q[6], array(3,4,5))) {
		$q[7] = mt_rand(3,5);
		$q[8] = getFixPercentResult($q8, 0.9, 3, round($panelSize*0.85), '8');
	} else {
		$q[7] = mt_rand(1,2);
		$q[8] = mt_rand(1,2);
	}
	
	//第10题
	//3-5 70%  1-2 30%
	$q[9] = getFixPercentResult($q9, 0.8, 3, $panelSize, '9');
	//第11题
	if (in_array($q[9], array(3,4,5))) {
		$q[10] = getFixPercentResult($q10, 0.9, 3, round($panelSize*0.8), '10');
	} else {
		$q[10] = mt_rand(1,2);
	}
	
	//第12题
	//3-5 65%
	$q[11] = getFixPercentResult($q11, 0.8, 3, $panelSize, '11');
	//第13题-第15题
	if (in_array($q[11], array(3,4,5))) {
		$q[12] = getFixPercentResult($q12, 0.95, 3, round($panelSize*0.8), '12');
		$q[13] = getFixPercentResult($q13, 0.85, 3, round($panelSize*0.8), '13');
	} else {
		$q[12] = mt_rand(1,2);
		$q[13] = mt_rand(1,2);
	}
	$q[11] = flip($q[11]);
	$q[12] = flip($q[12]);
	$q[13] = flip($q[13]);
	
	//第16题
	//2-5 50%
	$q[15] = getFixPercentResult($q15, 0.8, 3, $panelSize, '15');
	//第17题
	if (in_array($q[15], array(3,4,5))) {
		$q[16] = getFixPercentResult($q16, 0.85, 3, round($panelSize*0.8), '16');
	} else {
		$q[16] = mt_rand(1,2);
	}
	//第18题
	if (in_array($q[16], array(3,4,5))) {
		$q[17] = getFixPercentResult($q17, 0.98, 3, round($panelSize*0.8*0.85), '17');
	} else {
		$q[17] = mt_rand(1,2);
	}
	
	//第19题
	//3-5 80%
	$q[18] = getFixPercentResult($q18, 0.88, 3, $panelSize, '18');
	//第20题-第21题
	if (in_array($q[18], array(3,4,5))) {
		$q[19] = getFixPercentResult($q19, 0.9, 3, round($panelSize*0.88), '19');
		$q[20] = getFixPercentResult($q20, 0.9, 3, round($panelSize*0.88), '20');
	} else {
		$q[19] = mt_rand(1,2);
		$q[20] = mt_rand(1,2);
	}
	
	//第22题-第23题
	if (in_array($q[1], array(3,4,5))) {
		$q[21] = mt_rand(3, 5);
		$q[22] = mt_rand(3, 5);
	} else {
		$q[21] = mt_rand(1, 2);
		$q[22] = mt_rand(1, 2);
	}
	//第24题
	if (in_array($q[21], array(3,4,5))) {
// 		$q[23] = mt_rand(3, 5);
// 		echo 'ttt:', ++$ttt, "\n";
		$q[23] = getFixPercentResult($q23, 0.9, 3, round($panelSize*163/203), '23');
	} else {
		$q[23] = mt_rand(1, 2);
	}
	
	//第25题
	$q[24] = getAllFixPercentResult($q24, array(1=>2, 2=>8, 3=>90), $panelSize);
	if (3 == $q[24]) {
		$q[24] = mt_rand(3,5);
	}
	//第26题
	if (in_array($q[24], array(3,4,5))) {
		$q[25] = getFixPercentResult($q25, 0.98, 3, round($panelSize*0.9), '25');
	} else {
		$q[25] = mt_rand(1,2);
	}
	
	//第三十九题->51题->27题
	$q[26] = 'name';
	//第四十题->52题->28题
	$q[27] = getAllFixPercentResult($q27, array(1=>75, 2=>128), $panelSize);
	//第四十一题->53题->29题
	$q[28] = getAllFixPercentResult($q28, array(1=>482, 2=>453, 3=>30, 4=>20, 5=>15), $panelSize);
	//第四十二题->54题->30题
	$q[29] = getAllFixPercentResult($q29, array(1=>10, 2=>109, 3=>75, 4=>9), $panelSize);
	//第四十三题->55题->31题
	$q[30] = getAllFixPercentResult($q30, array(1=>20, 2=>42, 3=>91, 4=>50), $panelSize);
	
	//第32题
	if (in_array($q[10], array(3,4,5))) {
		$q[31] = mt_rand(3, 5);
	} else {
		$q[31] = mt_rand(1,2);
	}
	//第33题
	if (in_array($q[24], array(3,4,5))) {
		$q[32] = mt_rand(3, 5);
	} else {
		$q[32] = mt_rand(1,2);
	}
	//第34题
	if (1 == $q[2]) {
		$q[33] = 1;
	} elseif (2 == $q[2]) {
// 		echo '2:',++$ss, "\n";
		$q[33] = getAllFixPercentResult($q33_2, array(2=>44, 5=>6), round(50*$panelSize/203));
	} elseif (3 == $q[2]) {
// 		echo '3:',++$sss, "\n";
		$q[33] = getAllFixPercentResult($q33_3, array(3=>10, 4=>2, 5=>23), round(35*$panelSize/203));
	} elseif (4 == $q[2]) {
// 		echo '4:',++$ssss, "\n";
// 		$q[33] = getAllFixPercentResult($q33_4, array(4=>7, 5=>3), round(10*$panelSize/203));
		$q[33] = 4;
	} else {
		$q[33] = 5;
	}
	
// 	//Q34 3-5的概率
// 	$proQ34_3_5 = 0.95;
// 	//当Q3 3-5时 Q34 3-5的概率
// 	$proQ34_3_5_in_Q3_3_5 = 1;
// 	if (in_array($q[2], array(3,4,5))) {
// 		$q[33] = getFixPercentResult($q33_0, $proQ34_3_5_in_Q3_3_5, 3, 123);
// 	} else {
// 		//计算扣除Q3 3-5后，Q34还剩多少3-5的指标
// 		$remainQ34_3_5 = 203 * $proQ34_3_5 - 123 * $proQ34_3_5_in_Q3_3_5;
// 		$remainQ34_1_2 = 203 * (1-$proQ34_3_5) - 123 * (1-$proQ34_3_5_in_Q3_3_5);
// 		$y = getFixResult($q33_1, array(1=>round($remainQ34_1_2), 2=>round($remainQ34_3_5)), 2, 1);
// 		$q33_1[$y]++;
// 		if (1 == $y) {
// 			$q[33] = mt_rand(1, 2);
// 		} else {
// 			$q[33] = mt_rand(3, 5);
// 		}
// 	}
	//打印
	for ($m = 0; $m < 34; $m++) {
		if (14!=$m) {
			echo $q[$m];
			if ($m < 33) {
				echo ',';
			} else {
				echo "\n";
			}
		}
	}
}
exit();

function getFixResult($results, $limit, $max, $min, $sn) {
	$flag = true;
	$i = 0;
	do {
		$t = mt_rand($min,$max);
		if ($results[$t] < $limit[$t]) {
			$flag = false;
			return $t;
		} else {
			$flag = true;
		}
		if ($i++ >150000) {
			echo 'sn:', $sn, "\n";
			print_r($results);
			print_r($limit);
			echo $results[$t], "\t", $limit[$t], "\n";
			if ($i++ >150001) {
				exit();
			}
		}
	} while ($flag);
}

function getPercentResult($percent, $border) {
	$t = mt_rand(1, 100);
	if ($t <= $percent) {
		return mt_rand($border, 5);
	} else {
		return mt_rand(1, $border-1);
	}
}

function getFixPercentResult(&$results, $percent, $border, $total, $sn) {
	//将百分比转为数量
	$numBig = round($total * $percent);
	$numSmall = round($total - $numBig);
//	if ($flag) {
//		echo $numSmall, ' ', $numBig, "\n";
//	}
	$tr = getFixResult($results, array(1=>$numSmall, 2=>$numBig), 2, 1, $sn);
	$results[$tr]++;
	if (1 == $tr) {
		return mt_rand(1, $border-1);
	} else {
		return mt_rand($border, 5);
	}
}

function getAllFixPercentResult(&$results, $limit, $gross) {
	$limitValueSum = array_sum(array_values($limit));
	//生成各部分区间
	$startNow = 0;
	foreach ($limit as $k => $v) {
		$army[$k] = round($gross*$v/$limitValueSum);
		$section[$k] = array($startNow, $startNow+=$army[$k]);
	}
	$flag = true;
	$i = 0;
	do {
		$a = mt_rand(1, $gross);
		foreach ($section as $k => $v) {
			if ($v[0]<$a && $a<=$v[1]) {
				//判断该结果是否已经满员
				if ($results[$k] < $army[$k]) {
					$results[$k]++;
					return $k;
				}
			}
		}
		if ($i++ >150000) {
			print_r($limit);
			print_r($results);
			print_r($army);
			echo $results[$k], "\t", $army[$k], "\n";
			if ($i++ >150004) {
				exit();
			}
		}
	} while ($flag);
}

function flip($o) {
	return 6-$o;
}